//
//  LPActivityDetailReportTableViewCell.m
//  LovingPartyBuilding
//
//  Created by 刘应平 on 2017/6/6.
//  Copyright © 2017年 刘应平. All rights reserved.
//

#import "LPActivityDetailReportTableViewCell.h"
#import "LPActivityDetailTableFooterView.h"
#import "LPActivityDetailTableHeaderView.h"
#import "LPActivityDetailReportDetailTableViewCell.h"
#import "LPActivityDetailStudyNoDataTableViewCell.h"
#define NODATA @"nodata"
#define HASDATA @"hasdata"
@interface LPActivityDetailReportTableViewCell ()<UITableViewDelegate,UITableViewDataSource,LPActivityDetailFooterDelegate>
/**  table  */
@property (strong, nonatomic) UITableView  *tableView;
/**  tableHEIGHT  */
@property (assign, nonatomic) CGFloat  tableHeight;

@end
@implementation LPActivityDetailReportTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}


- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self layoutUI];
    }
    return self;
}


- (void)setModelArray:(NSArray *)modelArray{
    _modelArray = modelArray;
    
    if (modelArray.count != 0) {
        self.tableHeight = 110 * [UIScreen zj_screenHScale] + 130 * self.modelArray.count * [UIScreen zj_screenHScale];
    }else{
        if (self.isSecretary) {
            self.tableHeight = 190 * [UIScreen zj_screenHScale];
        }else{
            self.tableHeight = 130 * [UIScreen zj_screenHScale];
        }
    }
    
    CGRect frame = self.tableView.frame;
    frame.size.height = self.tableHeight;
    self.tableView.frame = frame;
    [self.tableView reloadData];
    [self addFooterAndHeader];
}


#pragma mark - 界面
- (void)layoutUI{
    [self addSubview:self.tableView];
    
}


#pragma mark - 私有方法
- (void)addFooterAndHeader{
    LPActivityDetailTableHeaderView *heaer = [[LPActivityDetailTableHeaderView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen zj_screenWith], 50 * [UIScreen zj_screenHScale]) buttonTitle:@"报道" buttonImgae:@"wxy_baodao_l"];
    self.tableView.tableHeaderView = heaer;
    
    LPActivityDetailTableFooterView *footer = [[LPActivityDetailTableFooterView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen zj_screenWith], 60 * [UIScreen zj_screenHScale])];
    footer.delegate = self;
    
    NSArray *titles;
    if (self.isSecretary) {
        if (self.modelArray.count == 0) {
           titles = @[@"发布报道"];
        }else{
            titles = @[@"发布报道", @"查看更多报道"];
        }
        footer.titles = titles;
        self.tableView.tableFooterView = footer;
        
    }else{
        titles = @[ @"查看更多报道"];
        footer.titles = titles;
        if (self.modelArray.count != 0) {
            self.tableView.tableFooterView = footer;
        }
    }
    
}

#pragma mark - LPActivityDetailFooterDelegate
- (void)lpActivityFooter_clickButton_index:(NSInteger)index{
    /**  查看更多的点击方法  */
    if (self.delegate) {
        [self.delegate LPActivityDetailReportTabelWithCell:self watchMore_index:index];
    }
}

#pragma mark - UITableViewDataSource,UITableViewDelegate
/**  返回组个数  */
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}
/**  返回cell个数  */
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (self.modelArray.count == 0) {
        return 1;
    }else{
        return self.modelArray.count;
    }
}
/**  返回cell高度  */
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (self.modelArray.count == 0) {
        return 80 * [UIScreen zj_screenHScale];
    }else{
        return 130 * [UIScreen zj_screenHScale];
    }
    
}
/**  cell初始化  */
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (self.modelArray.count == 0) {
         LPActivityDetailStudyNoDataTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NODATA forIndexPath:indexPath];
        cell.title.text = @"暂无报道";
        return cell;
    
    }else{

        LPActivityDetailReportDetailTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:HASDATA forIndexPath:indexPath];
        cell.model = self.modelArray[indexPath.row];
        if (indexPath.row == self.modelArray.count - 1) {
            cell.isLastCell = YES;
        }
        
        return cell;
    
    }
   
}
/**  cell的点击事件  */
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (self.modelArray.count != 0) {
        if (self.delegate) {
            [self.delegate LPActivityDetailReportTabelWithCell:self index:indexPath.row];
        }
        
    }
}

#pragma mark - 懒加载
/**  初始化tableView  */
- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen zj_screenWith], 130 * [UIScreen zj_screenHScale]) style:UITableViewStylePlain];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.separatorColor = [UIColor clearColor];
        _tableView.scrollEnabled = NO;
        [_tableView registerClass:[LPActivityDetailStudyNoDataTableViewCell class] forCellReuseIdentifier:NODATA];
        [_tableView registerClass:[LPActivityDetailReportDetailTableViewCell class] forCellReuseIdentifier:HASDATA];
    }
    return _tableView;
}


@end
